package com.wsj.servlet;

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "myServlet", urlPatterns = {"/hello"}, initParams = {@WebInitParam(name = "user", value = "houyangyang")}, loadOnStartup = 1)
public class MyServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//    super.doGet(req, resp);
    System.out.println("hello world");
    resp.setContentType("text/html");
    resp.setCharacterEncoding("utf-8");
    PrintWriter out = resp.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>登录出错</TITLE>");
    out.println("<meta http-equiv=\"refresh\" charset='utf-8' content=\"3;" + "\">");
    out.println("</HEAD>");
    out.println("  <BODY>");
    out.print("<div align=\"center\">");
    out.print("您输入的书名不存在或书名有错误，请重试");
    out.print("</div>");
    out.println("  </BODY>");
    out.flush();
    out.close();

    //req.getRequestDispatcher("/index.html").forward(req, resp);
  }

  //在web应用程序启动时，第一个请求到达servlet时调用，会花费一些时间加载init方法，可以设置load-on-startup属性保证在应用程序启动后立即加载servlet，只初始化一次
  @Override
  public void init() throws ServletException {
    super.init();
    //所有属性设置完成
    ServletConfig config = super.getServletConfig();
    //servletContext无法获取，属性设置在ServletConfig中
//    String userValue = config.getServletContext().getInitParameter("user");
    String userValue = config.getInitParameter("user");
    //设置Application级的属性,用于多个servlet之间传递共享数据
    config.getServletContext().setAttribute("password", "123");
    String password = (String) config.getServletContext().getAttribute("password");
    System.out.println(config.getServletName() + " started...");
    System.out.println("初始化参数完成：user=" + userValue + ",password=" + password);
  }

  @Override
  public void init(ServletConfig config) throws ServletException {
    //如果重写带参数init方法，则需要调用父类init方法，以便初始化ServletConfig
    super.init(config);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doGet(req, resp);
  }

  @Override
  public void destroy() {
    super.destroy();
    ServletConfig config = super.getServletConfig();
    System.out.println(config.getServletName() + " destroy...");
  }

  @Override
  protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.service(req, resp);
    Properties properties = new Properties();
    //web环境下读取文件
    //properties.load(new FileReader(this.getServletContext().getRealPath("/WEB-INF/classes/jdbc.properties")));
    properties.load(this.getServletContext().getResourceAsStream("/WEB-INF/classes/jdbc.properties"));
    System.out.println(properties.getProperty("jdbc.driverClassName"));
    System.out.println("处理请求");
  }
}
